import axios from 'axios'
import { Modal } from 'ant-design-vue'

// 创建 axios 实例
const service = axios.create({
  baseURL: '/api', // api base_url
  timeout: 15000 // 请求超时时间
})

service.interceptors.request.use(config => {
  const token = sessionStorage.getItem('token')
  if (token) {
    config.headers.Authorization = 'Bearer ' + token
  }
  return config
})

service.interceptors.response.use((response) => {
  if (response.request.responseType === 'blob') {
    return response
  }
  const resData = response.data
  const code = response.data.code
  if (code === 500) {
    Modal.error({
      title: '提示：',
      content: resData.message,
      okText: '重新登录',
      onOk: () => {
        sessionStorage.removeItem('token')
        window.location.reload()
      }
    })
  }
  return response
})

export {
  service as axios
}
